﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DiplomProjectRepository.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains DepartmentRepository class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace iBuproModel.Repositories.Classes
{
    using System;
    using System.Linq;

    using iBuproModel.Repositories.Interfaces;

    using System.Linq.Expressions;

    /// <summary>
    /// Implements IDiplomProjectRepository using linq to entities.
    /// </summary>
    public class DiplomProjectRepository : GenericRepository<DiplomProject>, IDiplomProjectRepository
    {
        public void AddDiplomProjectForLector(User lector, DiplomProject project)
        {
            lector.LectorDiplomProjects.Add(project);
        }
        
        new public IQueryable<DiplomProject> GetAll()
        {
            return context.Projects.OfType<DiplomProject>();
        }

        new public DiplomProject First(Expression<Func<DiplomProject, bool>> filter)
        {
            return context.Projects.OfType<DiplomProject>().First<DiplomProject>(filter);
        }

        new public DiplomProject GetSingle(Expression<Func<DiplomProject, bool>> filter)
        {
            return this.context.CreateObjectSet<DiplomProject>().OfType<DiplomProject>().SingleOrDefault<DiplomProject>(filter);
        }

       
        public void  UpdateDimplomProject(int id, DiplomProject project)
        {
            DiplomProject stored = First(p => p.Id == id);
            project.Id = id;

            context.Detach(stored);

            context.Attach(project);
        }

        public void DeleteDiplomProject(int id)
        {
            DiplomProject stored = First(p => p.Id == id);
            
            ITagRepository tagRepository = repositoryService.TagRepositoryCreateInstance();


            tagRepository.RemoveTagsForProject(id);
            stored.DepartmentPrincipal = null;
            stored.Lector = null;
            stored.ParentProject = null;
            stored.SourceProject = null;
            stored.Users = null;


            
            

            context.Projects.DeleteObject(stored);


        }
    }
}
